package com.taiji.workOrder.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import com.taiji.common.utils.DateUtils;
import com.taiji.common.utils.SecurityUtils;
import com.taiji.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.taiji.workOrder.mapper.ReportMapper;
import com.taiji.workOrder.domain.Report;
import com.taiji.workOrder.service.IReportService;

/**
 * 报告信息Service业务层处理
 *
 * @author taiji
 * @date 2023-08-08
 */
@Service
public class ReportServiceImpl implements IReportService {
    @Autowired
    private ReportMapper reportMapper;

    /**
     * 查询报告信息
     *
     * @param id 报告信息主键
     * @return 报告信息
     */
    @Override
    public Report selectReportById(String id) {
        return reportMapper.selectReportById(id);
    }

    /**
     * 查询报告信息列表
     *
     * @param report 报告信息
     * @return 报告信息
     */
    @Override
    public List<Report> selectReportList(Report report) {
        return reportMapper.selectReportList(report);
    }

    /**
     * 新增报告信息
     *
     * @param report 报告信息
     * @return 结果
     */
    @Override
    public int insertReport(Report report) {
        report.setId(IdUtils.randomUUID());
//        String name = report.getName();
//        report.setName(name.substring(0,name.lastIndexOf(".")));
        report.setCreateTime(DateUtils.getNowDate());
        report.setCreateBy(SecurityUtils.getUsername());
        return reportMapper.insertReport(report);
    }

    /**
     * 修改报告信息
     *
     * @param report 报告信息
     * @return 结果
     */
    @Override
    public int updateReport(Report report) {
        report.setUpdateTime(DateUtils.getNowDate());
        return reportMapper.updateReport(report);
    }

    /**
     * 批量删除报告信息
     *
     * @param ids 需要删除的报告信息主键
     * @return 结果
     */
    @Override
    public int deleteReportByIds(String[] ids) {
        /**
         *  删除前验证是否是本人所上传
         *  admin账户可以删除所有人的报告
         *  功能不是批量删除!!!!
         */
        String createBy = selectReportById(ids[0]).getCreateBy();
        String username = SecurityUtils.getUsername();
        if(createBy.equals(username) || username.equals("admin") ){
            return reportMapper.deleteReportByIds(ids);
        }
        return 999;
    }

    /**
     * 删除报告信息信息
     *
     * @param id 报告信息主键
     * @return 结果
     */
    @Override
    public int deleteReportById(String id) {
        return reportMapper.deleteReportById(id);
    }

    /**
     * 查询报告信息
     */
    @Override
    public Report selectReportByProjectCodeAndDate(Report report) {

        return reportMapper.selectReportByProjectCodeAndDate(report);
    }

    @Override
    public List<Report> selectReportListByDate(Report report) throws ParseException {
        String searchValue = report.getSearchValue();
        String[] split = searchValue.split("-");
        int year = Integer.parseInt(split[0]);
        int month = Integer.parseInt(split[1]);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = String.format("%04d-%02d-01", year, month);
        String endDate = String.format("%04d-%02d-%02d", year, month, getLastDayOfMonth(year, month));
        report.setCreateTime(dateFormat.parse(startDate));
        report.setUpdateTime(dateFormat.parse(endDate));


        List<Report> reports = reportMapper.selectReportListByDate(report);

        return reports;
    }

    @Override
    public List<Report> selectReportEnclosureList(Report report) {
        return reportMapper.selectReportEnclosureList(report);
    }

    // 获取指定年份和月份的最后一天
    private static int getLastDayOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
}